package com.ccctop.cloud.service.impl;

import com.ccctop.cloud.mapper.TContractAddressDao;
import com.ccctop.cloud.service.api.TContractAddressService;
import com.ccctop.crowd.entity.po.TContractAddress;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (TContractAddress)表服务实现类
 *
 * @author makejava
 * @since 2022-03-23 22:30:14
 */
@Service("tContractAddressService")
public class TContractAddressServiceImpl implements TContractAddressService {
    @Resource
    private TContractAddressDao tContractAddressDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public TContractAddress queryById(Integer id) {
        return this.tContractAddressDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<TContractAddress> queryAllByLimit(int offset, int limit) {
        return this.tContractAddressDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param tContractAddress 实例对象
     * @return 实例对象
     */
    @Override
    public TContractAddress insert(TContractAddress tContractAddress) {
        this.tContractAddressDao.insert(tContractAddress);
        return tContractAddress;
    }

    /**
     * 修改数据
     *
     * @param tContractAddress 实例对象
     * @return 实例对象
     */
    @Override
    public TContractAddress update(TContractAddress tContractAddress) {
        this.tContractAddressDao.update(tContractAddress);
        return this.queryById(tContractAddress.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.tContractAddressDao.deleteById(id) > 0;
    }

    @Override
    public TContractAddress queryByUserId(Integer projectId) throws Exception {
        TContractAddress query = new TContractAddress();
        query.setProjectId(projectId);
        List<TContractAddress> contract = this.tContractAddressDao.queryAll(query);
        if (contract.size()==1){
            return contract.get(0);
        }else {
            throw new Exception("此项目没有对应的智能合约");
        }
    }

    @Override
    public void contractAddressInsertOne(TContractAddress contractAddress) {
        tContractAddressDao.insert(contractAddress);
    }
}